#include "mysql_chat_session.hpp"
#include "mysql_friend_apply.hpp"
#include "mysql_relation.hpp"


void r_insert(xu::ManageRelation::ptr mr)
{
    mr->Insert("用户ID1","用户ID2");
    mr->Insert("用户ID1","用户ID3");
    mr->Insert("用户ID2","用户ID3");
}

void r_delete(xu::ManageRelation::ptr mr)
{
    mr->Delete("用户ID2","用户ID3");
}

void r_exists(xu::ManageRelation::ptr mr)
{
    std::cout<<mr->Exists("用户ID1","用户ID2")<<std::endl;
    std::cout<<mr->Exists("用户ID2","用户ID3")<<std::endl;   
    std::cout<<mr->Exists("用户ID1","用户ID3")<<std::endl;
}

void r_friend(xu::ManageRelation::ptr mr)
{
    auto res = mr->Friends("用户ID1");
    for(auto &e : res)
    {
        std::cout<<e<<std::endl;
    }
}

void f_insert(xu::ManageFriendApply::ptr mfa)
{
    xu::FriendApply f1("e1","用户ID1","用户ID2");
    xu::FriendApply f2("e2","用户ID2","用户ID3");
    xu::FriendApply f3("e3","用户ID1","用户ID3");

    mfa->Insert(f1);
    mfa->Insert(f2);
    mfa->Insert(f3);
}

void f_delete(xu::ManageFriendApply::ptr mfa)
{
    mfa->Delete("用户ID2","用户ID3");
    std::cout<<mfa->Exists("用户ID2","用户ID3")<<std::endl;
    std::cout<<mfa->Exists("用户ID1","用户ID2")<<std::endl;
    std::cout<<mfa->Exists("用户ID1","用户ID3")<<std::endl;
}

void f_friends(xu::ManageFriendApply::ptr mfa)
{
    auto res = mfa->ApplyUsers("用户ID2");
    for(auto &e : res)
    {
        std::cout<<e<<std::endl;
    }
}



void c_insert(xu::ManageChatSession::ptr mcs)
{
    xu::ChatSession cs1("会话ID1","会话名称1",xu::ChatSessionType::SINGLE);
    xu::ChatSession cs2("会话ID2","会话名称2",xu::ChatSessionType::GROUP);
    xu::ChatSession cs3("会话ID3","会话名称3",xu::ChatSessionType::SINGLE);
    xu::ChatSession cs4("会话ID4","会话名称4",xu::ChatSessionType::GROUP);

    mcs->Insert(cs1);
    mcs->Insert(cs2);
    mcs->Insert(cs3);
    mcs->Insert(cs4);

}

void c_select(xu::ManageChatSession::ptr mcs)
{
    auto res =mcs->Select("会话ID1");
    std::cout<<res->chat_session_id()<<std::endl;
    std::cout<<res->chat_session_name()<<std::endl;
    std::cout<<(int)res->chat_session_type()<<std::endl;
}

void c_one(xu::ManageChatSession::ptr mcs)
{
    auto res =mcs->GetSingleChatSession("用户ID1");
    for(auto &e : res)
    {
        std::cout<<e.chat_session_id<<std::endl;
        std::cout<<e.friend_id<<std::endl;
    }
}

void c_all(xu::ManageChatSession::ptr mcs)
{
    auto res =mcs->GetGroupChatSession("用户ID1");
    for(auto &e : res)
    {
        std::cout<<e.chat_session_id<<std::endl;
        std::cout<<e.chat_session_name<<std::endl;
    }
}

void c_delete(xu::ManageChatSession::ptr mcs)
{
    mcs->Delete("会话ID2");
    mcs->Delete("用户ID1","用户ID2");
}

int main()
{
    InitLog(false);
    auto db = xu::MysqlClientFactory::Create("root","xzyxzy99","xu","127.0.0.1","utf8");
    xu::ManageChatSession::ptr mcs = std::make_shared<xu::ManageChatSession>(db);
    xu::ManageFriendApply::ptr mfa = std::make_shared<xu::ManageFriendApply>(db);
    xu::ManageRelation::ptr mr = std::make_shared<xu::ManageRelation>(db);
    //r_insert(mr);
    //r_delete(mr);
    //r_exists(mr);
    //r_friend(mr);
    //f_insert(mfa);
    //f_delete(mfa);
    //f_friends(mfa);
    //c_insert(mcs);
    //c_select(mcs);
    //c_one(mcs);
    //c_all(mcs);
    c_delete(mcs);
    return 0;
}